package com.mallcai.bigdata.ladon.dpl.mapper.report.provider;

import com.google.common.base.Joiner;
import com.mallcai.bigdata.ladon.dpl.entity.report.ReportSql;
import com.mallcai.bigdata.ladon.dpl.mapper.BaseSqlProvider;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author gst
 * @date 2019-12-30 20:36
 */
public class ReportSqlSqlProvider extends BaseSqlProvider<ReportSql> {

    public static final String TB_REPORT_SQL = "r_report_sql";

    /**
     * 获取表名
     *
     * @return String
     */
    @Override
    public String getTableName() {
        return TB_REPORT_SQL;
    }

    /**
     * 获取插入数据的字段列表
     *
     * @return Map
     */
    @Override
    public Map<String, String> getInsertColumns() {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("ds_id", "#{dsId}");
        map.put("`cached`", "#{cached}");
        map.put("cached_time", "#{cachedTime}");
        map.put("timeout", "#{timeout}");
        map.put("text", "#{text}");
        map.put("need_detail_log", "#{needDetailLog}");
        map.put("comment", "#{comment}");
        return map;
    }

    /**
     * 获取更新操作的字段列表
     *
     * @return Map
     */
    @Override
    public Map<String, String> getUpdateColumns() {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("dsId", "ds_id");
        map.put("cached", "`cached`");
        map.put("cachedTime", "cached_time");
        map.put("timeout", "timeout");
        map.put("text", "text");
        map.put("needDetailLog", "need_detail_log");
        map.put("comment", "comment");
        return map;
    }

    /**
     * 根据参数查询
     */
    public String getByParams(Map<String, Object> map) {
        Map<String, Object> params = getParams(map);

        StringBuilder sb = new StringBuilder();
        sb.append("tb_r.archive = 0");

        if (params.containsKey("dsId") && params.get("dsId") != null) {
            sb.append(" AND tb_r.ds_id = #{dsId}");
        }
        if (params.containsKey("needDetailLog") && params.get("needDetailLog") != null) {
            sb.append(" AND tb_r.need_detail_log = #{needDetailLog}");
        }
        if (params.containsKey("cached") && params.get("cached") != null) {
            sb.append(" AND tb_r.cached = #{cached}");
        }
        if (params.containsKey("text") && StringUtils.isNotBlank(params.get("text").toString())) {
            sb.append(" and tb_r.text like CONCAT('%', #{params.text}, '%')");
        }
        return new SQL() {
            {
                SELECT("tb_r.*");
                FROM(getTableName() + " tb_r");
                WHERE(sb.toString());
            }
        }.toString();
    }

    public String delByIds(@Param("ids") List<Long> ids) {

        return new SQL() {
            {
                UPDATE(getTableName());
                SET(" archive = 1 ");
                WHERE(" id IN (" + Joiner.on(",").join(ids) + ")");
            }
        }.toString();
    }
}
